package archimede;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.miginfocom.swing.MigLayout;

import org.apache.commons.io.FileUtils;

import archimede.Upload.MyTransferListener;

public class Delete implements Runnable {
  private StartingWindow st;
  private String nameProject;
  private String directoryProject;

  public Delete(StartingWindow st, String nameproject, String directoryProject) {
    this.st = st;
    this.nameProject = nameproject;
    this.directoryProject = directoryProject;
  }

  @Override
  public void run() {
    // elimina in locale
    FTPClient client = new FTPClient();
    try {
      client.connect("83.211.158.68");
      client.login("stefano", "0901");

      client.changeDirectory("ProgettoArchimede");

      MyTransferListener transfer = new MyTransferListener();
      transfer.setStatus("Attendere");

      // lo metto in eliminati
      client.changeDirectory("ELIMINATI");
      try {
        client.deleteDirectory(nameProject);
      } catch (Exception er) {
        // non esiste una versione precedente eliminata
      }

      client.changeDirectoryUp();
      client.changeDirectory("AMMINISTRATORE");
      client.rename(nameProject, "../ELIMINATI/" + nameProject);

      // carico sul server
      // invio il proggetto in eliminati sezione EUREKA

      // client.logout();
      transfer.hideWindow();
      client.disconnect(true);

      // elimina in locale
      File dirtmp = new File(directoryProject);
      if (dirtmp.exists()) {
        try {
          // System.out.println("to delete ------> " + directoryProject);
          FileUtils.forceDelete(dirtmp);
        } catch (IOException e) {
          // System.out.println("error ------> " + directoryProject);
          e.printStackTrace();
        }
      }
      directoryProject = directoryProject + "_delete";
      dirtmp = new File(directoryProject);
      dirtmp.setWritable(false);
      dirtmp.mkdir();
      st.nuovo();
      st.setProjectListFromPC();

      transfer.hideWindow();

    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (FTPIllegalReplyException e) {
      e.printStackTrace();
    } catch (FTPException e) {
      e.printStackTrace();
    }
  }

  public class MyTransferListener extends JFrame implements
      FTPDataTransferListener {

    private JProgressBar bar;

    public MyTransferListener() {
      super("Download");
      try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (UnsupportedLookAndFeelException localUnsupportedLookAndFeelException) {
      } catch (ClassNotFoundException localClassNotFoundException) {
      } catch (InstantiationException localInstantiationException) {
      } catch (IllegalAccessException localIllegalAccessException) {
      }
      bar = new JProgressBar(0, 100);
      bar.setIndeterminate(true);
      bar.setPreferredSize(new Dimension(200, 40));
      bar.setStringPainted(true);
      setLayout(new MigLayout());
      add(new JLabel("Eliminazione progetto in corso"), "wrap");

      add(bar);
      pack();
      setLocation(st.getWidth() / 2 - getWidth() / 2, st.getHeight() / 2
          - getHeight() / 2);
      setVisible(true);
    }

    public void setStatus(String status) {
      bar.setString(status);
    }

    public void hideWindow() {
      setVisible(false);
    }

    public void started() {
    }

    public void transferred(int length) {
    }

    public void completed() {

    }

    public void aborted() {
    }

    public void failed() {
    }

  }
}
